import { CARD} from '../../constant';
import { cardCache,toast} from 'nc-lightapp-front';
import { DOCLIST, STATUS } from "../../../../common/constant";
import { byPkorgFiter, fiterDefdoc, fiterFrkm } from '../../../../common/util/fiterUtils';
let {getDefData } = cardCache;
/**
* @description: 表体编辑前事件
* @param: moduleId 区域编码
* @param: key 当前字段编码
* @return: 布尔 true表示可编辑
*/
export function beforeTableEvent(props, moduleId, key, value, index, record, status) {

        let pk_accountingbook = null;
        let hdef7 = null;
        let where = null;
        let def1 = null;
        let pk_org = null;
        let fiterType = null;
        let isMultiSelectedEnabled = false;

        switch (key) {
                case 'acc'://法人会计科目
                        pk_accountingbook = getDefData('accbook', 'accbook');
                        pk_org = props.form.getFormItemsValue('card_head', 'pk_org').value;
                        fiterType = DOCLIST.qtcbjf;
                        fiterFrkm(props, moduleId, key, pk_org, pk_accountingbook, fiterType);
                        break;

                case 'def3'://费用承担部门
                        hdef7 = props.form.getFormItemsValue('card_head', 'def7').value;
                        if(hdef7 == ''){
                                toast({ color: STATUS.warning, content: '请先维护费用承担利润中心！' });
                                return false;
                        }
                        where = "def1 in (select pk_org from org_orgs where dr=0 and name in (select name from org_orgs where pk_org='"+hdef7+"')) and def7='Y'";
                        
                        fiterDefdoc(props, moduleId, key, where);
                        break;

                case 'def4'://费用明细
                        def1 = props.cardTable.getValByKeyAndIndex(CARD.table_code, index, 'def1').value;
                        if(def1 == ''){
                                toast({ color: STATUS.warning, content: '请先维护费报销类型！' });
                                return false;
                        }
                        where = "def11 = '"+def1+"'";

                        fiterDefdoc(props, moduleId, key, where);

                        break;

                case 'def7'://客商
                        pk_org = props.form.getFormItemsValue(CARD.form_id, 'pk_org').value;
                        byPkorgFiter(props, moduleId, key, pk_org, isMultiSelectedEnabled);
                        break;

                case 'def10'://人员档案
                        pk_org = props.form.getFormItemsValue(CARD.form_id, 'pk_org').value;
                        byPkorgFiter(props, moduleId, key, pk_org, isMultiSelectedEnabled);
                        break;

                case 'def2'://管理会计科目：利润中心科目LRZX001
                        def1 = props.cardTable.getValByKeyAndIndex(CARD.table_code, index, 'def1').value;
                        where = "def11 = '"+def1+"'";
                        if(def1 == ''){
                                toast({ color: STATUS.warning, content: '请先维护报销类型！' });
                                return false;
                        }
                        
                        fiterDefdoc(props, moduleId, key, where);
                        break;
        
                default:
                        break;
        }

        return true;
}
